package com.example.hello.hello.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.example.hello.hello.R;

public class Loading extends Dialog {

	private TextView _message;
	
	public Loading(Context context, int theme) {
		super(context, theme);
		init(context);
	}

	protected Loading(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init(context);
	}

	public Loading(Context context) {
		super(context);
		init(context);
	}
	
	public void init(Context context){
		setCancelable(false);
/*		设置setCancelable(true)时，点击ProgressDialog以外的区域的时候ProgressDialog就会关闭，反之设置setCancelable(false)时，点击ProgressDialog以外的区域不会关闭ProgressDialog*/		requestWindowFeature(Window.FEATURE_NO_TITLE);
		View view = LayoutInflater.from(context).inflate(R.layout.dialog_wait, null);
		_message = (TextView)view.findViewById(R.id.waiting_tv);
		setContentView(view);
	}
	public static void show(Context context) {
		if (context instanceof DialogControl)
			((DialogControl) context).showWaitDialog();
	}
	
	public static void hide(Context context) {
		if (context instanceof DialogControl)
			((DialogControl) context).hideWaitDialog();
	}

	public static boolean hide(Loading dialog) {
		if (dialog != null) {
			dialog.hide();
			return false;
		} else {
			return true;
		}
	}
	public static boolean dismiss(Loading dialog) {
		if (dialog != null) {
			dialog.dismiss();
			return false;
		} else {
			return true;
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
//		if (TDevice.isTablet()) {
//			int i = (int) TDevice.dpToPixel(360F);
//			if (i < TDevice.getScreenWidth()) {
//				WindowManager.LayoutParams params = getWindow()
//						.getAttributes();
//				params.width = i;
//				getWindow().setAttributes(params);
//			}
//		}
		
	}
	public void setMessage(int message) {
		_message.setText(message);
	}

	public void setMessage(String message) {
		_message.setText(message);
	}
	
	public void hideMessage() {
		_message.setVisibility(View.GONE);
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		this.dismiss();
	}
	
}
